﻿// -----------------------------------------------------------------------
// <copyright file="Map.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace BloodThirsty
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class Map
    {


        private Texture2D[] _map2D;

        public Texture2D[] Map2D
        {
            get { return _map2D; }
            set { _map2D = value;
            iTexture = 0;
            nTexture = _map2D.Length;
            
            }
        }


        private double _Left;

        public double Left
        {
            get { return _Left; }
            set { _Left = value; }
        }

        private double _Top;

        public double Top
        {
            get { return _Top; }
            set { _Top = value; }
        }

        private int _iTexture;

        public int iTexture
        {
            get { return _iTexture; }
            set { _iTexture = value; }
        }

        

        private int _nTexture;

        public int nTexture
        {
            get { return _nTexture; }
            set { _nTexture = value; }
        }

        private int _huongChuyenDongMap;

        public int HuongChuyenDongMap
        {
            get { return _huongChuyenDongMap; }
            set { _huongChuyenDongMap = value; }
        }

        public Map(Texture2D[] map, double VitribandauX,double VitribandauY)
        {

            Map2D = map;
            Left = VitribandauX;
            Top = VitribandauY;
            
            
        }



        //Ham xac dinh goc trai man hinh dang o dau
        // Ham nay tra ve 1 itexture
        //Tim vi tri goc trai man hinh sau do ve them 1 so hinh nua cho day man hinh
        //De gioi han duong di cua man hinh
        public int ViTriGocTraiManHinh(double left, double top)
        {
            int VitriTraVe = 0;
            for(int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    if (( i * 512>=left) && (top >= j * 512))
                    {
                        VitriTraVe = (i * 8) + j;
                    }
                }
            }

            return VitriTraVe;
        }
        // Left va top trong class nay tuong trung cho vi tri man hinh may tinh tren Map

        // Ham nay dung de xac dinh itexture  nao dang co left va top nam tren do
        
        public void XacDinhVitriChoMap(int i)
        {
            
            int a=8;
            if (i < a)
            {
                Left = i * 512;
                Top = 0;
                
            }
            else
            {
                if (i < a * 2)
                {
                    Left = (i - a) * 512;
                    Top = 512;
                    

                }
                else
                {
                    if (i < a * 3)
                    {
                        Left = (i - (a*2)) * 512;
                        Top = 512*2;
                       
                    }
                    else
                    {
                        if (i < a * 4)
                        {
                            Left = (i - (a * 3)) * 512;
                            Top = 512 * 3;

                        }
                        else
                        {
                            if (i < a * 5)
                            {
                                Left = (i - (a * 4)) * 512;
                                Top = 512 * 4;
                            }
                            else
                            {
                                if (i < a * 6)
                                {
                                    Left = (i - (a*5)) * 512;
                                    Top = 512 * 5;
                                }
                                else
                                {
                                    if (i < a * 7)
                                    {
                                        Left = (i - (a*6)) * 512;
                                        Top = 512 * 6;
                                    }
                                    else
                                    {
                                        Left = (i - (a*7)) * 512;
                                        Top = 512 * 7;
                                    }
                                }
                            }
                            
                        }
                    }
                }
            }
           

        }
        static bool ThayDoiHuong;
        int Tam = 0;
        int BuocChayNhanVat= 1;
        public void Update(GameTime gameTime)
        {
            
            if (HuongChuyenDongMap != Tam)
            {
                ThayDoiHuong = true;
                BuocChayNhanVat = 1;
                Tam = HuongChuyenDongMap;
            }
            else
            {
                BuocChayNhanVat = BuocChayNhanVat + 2;
                ThayDoiHuong = false;
            }
        }
        
        public void XacDinhHuongChayNhanVat(int ChuyenDong)
        {
            
            if (ChuyenDong == 1)// Di ve ben phai
            {
                Left = Left - (BuocChayNhanVat *2);
                
                
            }
            if (ChuyenDong == 3)// Di len nua tren ben phai
            {
                Left = Left - BuocChayNhanVat;
                Top = Top + BuocChayNhanVat;
                

            }
            if (ChuyenDong == 2) //Di len tren
            {
                Top = Top + BuocChayNhanVat;
                
               
            }
            if (ChuyenDong == 7) //Di len nua tren ben trai
            {
                Left = Left + BuocChayNhanVat;
                Top = Top + BuocChayNhanVat;
            }
            if (ChuyenDong == 6) // Di ve ban trai
            {
                
                Left = Left + (BuocChayNhanVat * 2);
            }
            if (ChuyenDong == 8) //Di ve nua duoi ben trai
            {
                Left = Left + BuocChayNhanVat;
                Top = Top - BuocChayNhanVat;
            }
            if (ChuyenDong == 4) //Di xuong duoi
            {
                Top = Top - BuocChayNhanVat;
            }
            if (ChuyenDong == 5) //Di ve nua duoi ben phai
            {
                Top = Top - BuocChayNhanVat;
                Left = Left - BuocChayNhanVat;
            }
            
        }
        
        //int h = 0;
        Point ViTriManHinh = new Point(0, 0);
        double CapNhatTop, CapNhatLeft;
       

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {

                if (ThayDoiHuong == true)
                {
                    ViTriManHinh.X = (int)CapNhatLeft;
                    ViTriManHinh.Y = (int)CapNhatTop;
                }
                
                for (iTexture = 0; iTexture < nTexture; iTexture++)
                {
                    XacDinhVitriChoMap(iTexture);
                    Left = Left - ViTriManHinh.X;
                    Top = Top - ViTriManHinh.Y;
                    XacDinhHuongChayNhanVat(HuongChuyenDongMap);


                    spriteBatch.Draw(Map2D[iTexture], new Vector2((float)Left, (float)Top), Color.White);

                    if (iTexture == 0)
                    {
                        CapNhatLeft = 0 - Left;
                        CapNhatTop = 0 - Top;

                    }

                }
                

               
            
           
        }
    }
}
